#ifndef TEST_MAP_H
#define TEST_MAP_H

#include <glad/glad.h>

#include <QQuickItem>

#include <algorithm/analytical_model/algorithm/cuda/inter_op/Data.h>

namespace solar
{
    class MapRenderer;
    class Map : public QQuickItem
    {
            Q_OBJECT
            Q_PROPERTY(qreal t READ t WRITE setT NOTIFY tChanged)
            Q_PROPERTY(int display_x MEMBER display_x_ NOTIFY displayXChanged)
            Q_PROPERTY(int display_y MEMBER display_y_ NOTIFY displayYChanged)
            Q_PROPERTY(int display_width MEMBER display_width_ NOTIFY displayWidthChanged)
            Q_PROPERTY(int display_height MEMBER display_height_ NOTIFY displayHeightChanged)

            Q_PROPERTY(int img_resolution_x MEMBER img_resolution_x_)
            Q_PROPERTY(int img_resolution_y MEMBER img_resolution_y_)
            Q_PROPERTY(int img_width MEMBER img_width_ NOTIFY imgWidthChanged)
            Q_PROPERTY(int img_height MEMBER img_height_ NOTIFY imgHeightChanged)
            Q_PROPERTY(int img_boundary_horizontal_dst MEMBER img_boundary_horizontal_dst_)

            Q_PROPERTY(int x_offset MEMBER x_offset_ NOTIFY xOffsetChanged)
            Q_PROPERTY(int y_offset MEMBER y_offset_ NOTIFY yOffsetChanged)

            QML_ELEMENT

        public:
            explicit Map(QQuickItem* parent = nullptr);
            [[nodiscard]] auto t() const -> qreal { return t_; }
            void setT(qreal t);

        signals:
            void tChanged();
            void displayXChanged();
            void displayYChanged();
            void displayWidthChanged();
            void displayHeightChanged();
            void imgWidthChanged();
            void imgHeightChanged();
            void xOffsetChanged();
            void yOffsetChanged();

            void mapCompletedSignal(); // 当cuda画出图像后发出该信号---把colorbar及axis设置为visible

            // void updateImage();

        public slots:
            void sync();
            void cleanup();
            void windowChanged(QQuickWindow* win);
            void releaseResources() override;
            /**
             * TODO::updateImage这里是为了connect
             * Maprenderer中的reloadTexture，但是并不起作用，只能在每次paint时都加载一次texture
             */
            void updateImage();
            void mapCompleted();

            // d_data是显卡中的地址，存的是生成纹理图片需要的结果数据（smooth之后的数据）
            Q_INVOKABLE void generateTexture(cuda::inter_op::Uchar4* fluxmap_data, int resolution_x,
                                             int resolution_y);
            Q_INVOKABLE void setVisible(bool visible);
            Q_INVOKABLE void setScale(qreal scale);
            Q_INVOKABLE void setPos(int x_offset, int y_offset);
            // void updateImage(const QImage&);

        protected:
            void setImageSize();
            // QSGNode* updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData *) override;

        private:
            qreal t_;
            // QPointF m_p1{0.0, 0.0}, m_p2{1,0}, m_p3{0,1}, m_p4{1,1};
            // int m_segmentCount = 32;
            MapRenderer* renderer_;

            int display_x_, display_y_, display_width_,
                display_height_; // map所处的显示部分的位置及大小（这里即datavisuallizationwindow的右半部分）
            int img_resolution_x_, img_resolution_y_; // 图片实际的分辨率（原图的分辨率）
            int img_width_, img_height_;              // 界面上图片的宽高（经过调整）
            int img_boundary_horizontal_dst_; // 图片与边界的水平方向上的距离---注意要使左右两边的axis和colorbar不被遮挡
            int x_offset_, y_offset_;         //

            bool map_completed_ = false; // 标记是否已经画好图像
            // 标记是否已经通过setImageSize首次设置过x_offset_和y_offset_，若已经设置过，则再执行setImageSize时，就不再调整offset
            bool already_first_set_offset_ = false;
    };
} // namespace solar

#endif
